package chapter11;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-22 下午2:13
 */
public class FileTestDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileTestDemo.class);

    public static void main(String[] args) {
        File file = new File(File.separator + "home" + File.separator + "lzy" + File.separator + "logs" + File.separator + "test" + File.separator + "test.dat");

        if (file.exists()) {
            LOGGER.info("is it a file:{}", file.isFile());
            LOGGER.info("is it a directory:{}", file.isDirectory());

            LOGGER.info("file size:{} M", new BigDecimal((double) file.length() / 1024 / 1024).divide(new BigDecimal(1), 2, BigDecimal.ROUND_HALF_UP));

            LOGGER.info("last edit: {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified())));
        }

        if (!file.getParentFile().exists()) {
            LOGGER.info("create directory {}:{}", file.getParent(), file.getParentFile().mkdirs());
        }

        if (file.exists()) {
            LOGGER.info("delete file {}:{}", file.getName(), file.delete());
        } else {
            try {
                LOGGER.info("create new file {}", file.createNewFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
